package com.gxa.community.customer.controller.cart;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.community.customer.service.cart.CartService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("查询当前登录用户的购物车信息")
    @GetMapping("/querycartinfo")
    public String queryCartInfo(Integer userId) throws JsonProcessingException {
        String result = cartService.queryCartInfo(userId);
        return result;
    }

    @ApiOperation("当前用户向购物车里添加商品")
    @PostMapping("/addcart")
    public String addCart(@RequestParam("userId") Integer userId,@RequestParam("goodsId")Integer goodsId,@RequestParam("goodsQuantity")Integer goodsQuantity) throws JsonProcessingException {
        String s = cartService.addCart(userId, goodsId, goodsQuantity);
        return s;
    }

    @ApiOperation("当前用户增加购物车里的商品数量（点击+号，增加1)")
    @PutMapping("/addcartgoodsquantityby1")
    public String addCartGoodsQuantityBy1(@RequestParam("userId")Integer userId,@RequestParam("goodsId")Integer goodsId) throws JsonProcessingException {

        return cartService.addCartGoodsQuantityBy1(userId,goodsId);
    }

    @ApiOperation("当前用户减少购物车里的商品数量（点击-号，减去1)")
    @PutMapping("/delcartgoodsquantityby1")
    public String delCartGoodsQuantityBy1(@RequestParam("userId")Integer userId,@RequestParam("goodsId")Integer goodsId) throws JsonProcessingException {

        return cartService.delCartGoodsQuantityBy1(userId,goodsId);
    }

    @ApiOperation("当前用户批量删除购物车里的商品（编辑商品按钮功能接口）")
    @DeleteMapping("/delcartgoodsbatch")
    public String delCartGoodsBatch(@RequestParam("userId")Integer userId,@RequestParam("goodsIds")Integer...goodsIds) throws JsonProcessingException {
        log.info("userId=============>{}",userId);
        log.info("goodsIds=============>{}",goodsIds);
        return cartService.delCartGoodsBatch(userId,goodsIds);
    }

    @ApiOperation("修改购物车中商品选中状态")
    @PutMapping("/updatecheckedstatus")
    public String updateCheckedStatus(@RequestParam("userId")Integer userId,@RequestParam("goodsId")Integer goodsId,@RequestParam("checked")Integer checked) throws JsonProcessingException {

        return cartService.updateCheckedStatus(userId, goodsId, checked);
    }
}
